//
//  MCPersonInformationViewController.m
//  TLYL
//
//  Created by MC on 2017/6/14.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCPersonInformationViewController.h"
#import "MCPersonInformationTableViewCell.h"
#import "MCPersonInformationHeaderView.h"
#import "MCPersonInformationFooterView.h"
#import "MCMineCellModel.h"
#import "MCMineInfoModel.h"
#import "MCModifyUserInfoModel.h"
#import "MCBandRealNameModel.h"
#import "NSString+Helper.h"
@interface MCPersonInformationViewController ()
<
UITableViewDelegate,
UITableViewDataSource,
MCPersonInformationFooterDelegate
>

@property(nonatomic, strong)UITableView   *tableView;
@property(nonatomic, strong)NSMutableArray*sectionMarr;
@property(nonatomic, strong)MCPersonInformationHeaderView *headerView;
@property(nonatomic, strong)MCPersonInformationFooterView *footerView;


/**邮箱*/
@property(nonatomic, weak)UITextField * emailTextField;
/**手机号*/
@property(nonatomic, weak)UITextField * phoneNumberTextField;

@property(nonatomic, strong)MCMineInfoModel * mineInfoModel;
@property(nonatomic, strong)MCModifyUserInfoModel * modifyUserInfoModel;

@end

@implementation MCPersonInformationViewController

#pragma mark-headerView
-(MCPersonInformationFooterView *)footerView{
    if (!_footerView) {
        _footerView=[[MCPersonInformationFooterView alloc]init];
        _footerView.delegate=self;
    }
    return _footerView;
}

-(MCPersonInformationHeaderView *)headerView{
    if (!_headerView) {
        _headerView=[[MCPersonInformationHeaderView alloc]init];
    }
    return _headerView;
}
#pragma mark-viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setProperty];
    
    [self createUI];
    
    [self loadData];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.translucent = NO;
}


#pragma mark==================setProperty======================
-(void)setProperty{
    
    self.view.backgroundColor=RGB(239, 246, 252);
    self.title=@"个人资料";
    _sectionMarr= [[NSMutableArray alloc]init];
    
}

#pragma mark==================createUI======================
-(void)createUI{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    // 键盘会当tableView上下滚动的时候自动收起
    _tableView.keyboardDismissMode=UIScrollViewKeyboardDismissModeOnDrag;
    
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(0);
        make.left.and.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(0);
    }];
    
    UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyboard:)];
    singleTapGesture.numberOfTapsRequired = 1;
    singleTapGesture.cancelsTouchesInView = NO;
    [_tableView addGestureRecognizer:singleTapGesture];
    

}

#pragma mark - gesture actions
- (void)closeKeyboard:(UITapGestureRecognizer *)recognizer {
    [self.view endEditing:YES];
}

#pragma mark==================loadData======================
-(void)loadData{
    
    MCMineInfoModel *mineInfoModel = [MCMineInfoModel sharedMCMineInfoModel];
    self.mineInfoModel = mineInfoModel;
    
    
    CellModel* model0 =[[CellModel alloc]init];
    model0.reuseIdentifier = NSStringFromClass([MCPersonInformationTableViewCell class]);
    model0.className=NSStringFromClass([MCPersonInformationTableViewCell class]);
    model0.height = [MCPersonInformationTableViewCell computeHeight:nil];
    model0.selectionStyle=UITableViewCellSelectionStyleNone;
    model0.accessoryType=UITableViewCellAccessoryNone;
    /*
     * 传递参数
     */
    model0.userInfo = nil;
    
    SectionModel *Smodel0=[SectionModel sectionModelWithTitle:@"" cells:@[model0]];
    Smodel0.headerhHeight=[MCPersonInformationHeaderView computeHeight:nil];
    Smodel0.footerHeight=80;
    [_sectionMarr addObject:Smodel0];
    
    [_tableView reloadData];
    [self.footerView relayOutUI];
    
    __weak __typeof__ (self) wself = self;
    [mineInfoModel refreashDataAndShow];
    mineInfoModel.callBackSuccessBlock = ^(id manager) {
        
        wself.mineInfoModel=[MCMineInfoModel mj_objectWithKeyValues:manager];
        [wself.tableView reloadData];
        [wself.footerView relayOutUI];
    };
    
}

#pragma mark tableView 代理相关
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _sectionMarr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    SectionModel *sm = _sectionMarr[section];
    return sm.cells.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    SectionModel *sm = _sectionMarr[section];
    return sm.headerhHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    SectionModel *sm = _sectionMarr[section];
    return sm.footerHeight;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return self.headerView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return self.footerView ;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    SectionModel *sm = _sectionMarr[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    
    return cm.height;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SectionModel *sm = _sectionMarr[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cm.reuseIdentifier];
    if (!cell) {
        cell = [[NSClassFromString(cm.className) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cm.reuseIdentifier];
    }
    cell.selectionStyle = cm.selectionStyle;
    
   
    if ([cm.className isEqualToString:NSStringFromClass([MCPersonInformationTableViewCell class])]) {
        
        MCPersonInformationTableViewCell *ex_cell=(MCPersonInformationTableViewCell *)cell;
        ex_cell.dataSource=cm.userInfo;
        _emailTextField=ex_cell.emailTextField;
        _phoneNumberTextField=ex_cell.phoneTextField;
        
    }
    
   
    return cell;
}


#pragma mark-MCPersonInformationFooterDelegate /保存/
-(void)savePersonalInformation{
    

    if ([_emailTextField.text isEqualToString:@""]||!_emailTextField.text) {
//        [SVProgressHUD showErrorWithStatus:@"请输入邮箱！"];
//        return;
    }else{
        if (![self isValidateEmail:_emailTextField.text]) {
            [SVProgressHUD showErrorWithStatus:@"请输入正确的邮箱！"];
            return;
        }
    }
    if([_phoneNumberTextField.text rangeOfString:@"*"].location !=NSNotFound){
        
    }else{
        if (_phoneNumberTextField.text.length>0) {
            if (![_phoneNumberTextField.text isValidateMobile]) {
                [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号！"];
                return;
            }
        }
    }
    
    
    MCModifyUserInfoModel * modifyUserInfoModel=[[MCModifyUserInfoModel alloc]init];
    self.modifyUserInfoModel=modifyUserInfoModel;
    
    modifyUserInfoModel.MobilePhone=_phoneNumberTextField.text;
    modifyUserInfoModel.EMail=_emailTextField.text;
    [modifyUserInfoModel refreashDataAndShow];
    
  
    
    
    modifyUserInfoModel.callBackFailedBlock = ^(ApiBaseManager *manager, NSString *errorCode) {
    };
    
    modifyUserInfoModel.callBackSuccessBlock = ^(ApiBaseManager * manager) {
        if (manager.responseMessage) {
            NSLog(@"%@-----manager",manager.responseMessage);
             [SVProgressHUD showSuccessWithStatus:manager.responseMessage];
        }
        [self.navigationController popViewControllerAnimated:YES];
    };

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}
//利用正则表达式验证
- (BOOL)isValidateEmail:(NSString *)email
{
    NSString *emailRegex =@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}


@end
